﻿$(function () {
    /**
     * 后台填充selector
     */
    $('.selectpicker').selectpicker({ noneSelectedText: '==请选择==', maxOptions:1 });
    let dpt_selector = $('select.selectpicker[name="dpt_name"]'),
        position_selector = $('select.selectpicker[name="position"]');
    if (dpt_selector.length) {
        let data = {
            _csrf: $("meta[name=x-csrf-token]").attr("content"),
            queryID: "#dpt"
        };
        $.post("/querydb", data, function (data) {
            data = JSON.parse(data);
            if (data.code == 200) {
                data.data.forEach(function (rowObj, i) {
                    dpt_selector.append("<option>" + rowObj.dpt_name + "</option>");
                })
                dpt_selector.val(dpt_selector.attr("data-value"));
                dpt_selector.selectpicker("refresh");
            }
            else {
                showModal("系统错误", "错误号:" + data.data.code + "\r\n错误信息:" + data.data.errmsg);
            }
        });
    }
    if (position_selector.length) {
        let data = {
            _csrf: $("meta[name=x-csrf-token]").attr("content"),
            queryID: "#position"
        };
        $.post("/querydb", data, function (data) {
            data = JSON.parse(data);
            if (data.code == 200) {
                data.data.forEach(function (rowObj, i) {
                    position_selector.append("<option>" + rowObj.position + "</option>");
                })
                position_selector.val(position_selector.attr("data-value"));
                position_selector.selectpicker("refresh");
            }
            else {
                showModal("系统错误", "错误号:" + data.data.code + "\r\n错误信息:" + data.data.errmsg);
            }
        });
    }
    /**
     * 为各控件添加数据校验
     */
    $('input.form-control[name="confirm_password"]').change(function (e) {
        let psw = $('input.form-control[name="password"]').val(),
            fgp = $(e.target).closest('.form-group');
        fgp.children('span').remove();
        if (e.target.value != psw && psw != "") {
            $(e.target).val("");
            fgp.addClass('has-error');
            fgp.append("<span class='alert-danger'>两次密码输入不一致</span>")
        }
        else {
            fgp.removeClass('has-error');
        }
    });
    $('input.form-control[name="password"]').change(function (e) {
        let psw = $('input.form-control[name="confirm_password"]').val(),
            fgp = $(e.target).closest('.form-group');
        fgp.children('span').remove();
        if (e.target.value != psw && psw != "") {
            $(e.target).val("");
            fgp.addClass('has-error');
            fgp.append("<span class='alert-danger'>两次密码输入不一致</span>")
        }
        else {
            fgp.removeClass('has-error');
        }
    });
})
/**
 * 数据提交前验证数据完整性的方法
 */
var pre_submit_validate = function () {
    let vali = true,
        fgp = $('.form-group').children('span').remove(),
        psw_check_patt = /^[a-zA-Z0-9_@#$]{8,16}$/,
        requiredElems = {
            username: {
                dom_type: "input",
                required: { msg: "帐号" }
            },
            old_psw: {
                dom_type: "input",
                required: { msg: "原密码" }
            },
            password: {
                dom_type: "input",
                required: { msg: "密码" },
                match: {
                    msg: "密码必需由8-16位的数字字母或'_@#$'组成",
                    pattern: psw_check_patt
                }
            },
            confirm_password: {
                dom_type: "input",
                required: { msg: "确认密码" },
                match: {
                    msg: "密码必需由8-16位的数字字母或'_@#$'组成",
                    pattern: psw_check_patt
                },
                eqlTo: {
                    msg: "两次密码不一致",
                    tar_dom_type: "input",
                    tar_selector: "input[name='password']"
                }
            },
            emp_name: {
                dom_type: "input",
                required: { msg: "姓名" }
            },
            emp_no: {
                dom_type: "input",
                required: { msg: "工号" },
                match: {
                    msg: "工号必需由数字组成",
                    pattern: /^[0-9]{1,}$/
                }
            },
            dpt_name: {
                dom_type: "select",
                required: { msg: "部门" }
            },
            position: {
                dom_type: "select",
                required: { msg: "职位" }
            }
        },
        addAlertSpan = function (sender, selector, msg) {
            let gp = sender.closest(selector);
            if (!gp[0].classList.contains('has-error')) { gp.addClass('has-error'); }
            if (gp.children("span").length > 0) { msg = "；" + msg }
            gp.append("<span class='alert-danger'>" + msg + "</span>");
        };
    for (let prop in requiredElems) {
        let k = prop,
            v = requiredElems[prop],
            input = $(v.dom_type + '[name="' + k + '"]'),
            verb = v.dom_type == "select" ? "选择" : "输入",
            unexpect_val = v.dom_type == "select" ? "" : "",
            act_val = v.dom_type == "select" ? input.val() || "" : input.val();
        if (input.length <= 0) { continue; } // 如果控件不存在则跳过，进入下个循环
        for (let vali_con in v) {
            switch (vali_con) {
                case "required": //必需字段
                    if (act_val == unexpect_val) {
                        vali = false;
                        addAlertSpan(input, '.form-group', "必需" + verb + v[vali_con].msg);
                    }
                    break;
                case "eqlTo": // 比较字段
                    // 如果eqlTo属性的值不是数组则转化成单元素数组
                    if (!(v[vali_con] instanceof Array)) { v[vali_con] = [v[vali_con]]; }
                    v[vali_con].forEach(function (eql_val, idx) {
                        var expectEqlVal = eql_val.tar_dom_type == "select" ?
                            $(eql_val.tar_selector).val() || "" :
                            $(eql_val.tar_selector).val();
                        if (act_val != expectEqlVal) {
                            vali = false;
                            addAlertSpan(input, '.form-group', eql_val.msg);
                        }
                    });
                    break;
                case "match": // 正则匹配字段
                    // 如果match属性的值不是数组则转化成单元素数组
                    if (!(v[vali_con] instanceof Array)) { v[vali_con] = [v[vali_con]]; }
                    v[vali_con].forEach(function (eql_val, idx) {
                        if (!act_val.match(eql_val.pattern)) {
                            vali = false;
                            addAlertSpan(input, '.form-group', eql_val.msg);
                        }
                    });
                    break;
            }
        }
    }
    return vali;
}